package Framework;

import java.awt.Component;
import java.util.HashMap;
import java.util.Stack;

import nl.hanze.t23i.gamemodule.extern.AbstractGameModule;

public abstract class Intelligence extends AbstractGameModule {

	public static final int EMPTY = 0;
	public static final int HUMAN = 1;
	public static final int COMPUTER = 2;

	public static final int HUMAN_WIN = 0;
	public static final int DRAW = 1;
	public static final int UNCLEAR = 2;
	public static final int COMPUTER_WIN = 3;

	public static int rows;
	public static int columns;

	public int[][] board;

	public int side = 0;

	private int position = UNCLEAR;
	protected boolean calculating = false;
	private String moveDetails;
	private HashMap<String, Integer> playerResults;
	private String nextPlayer;

	public Intelligence(int rows, int columns, String playerOne,
			String playerTwo, int side) {
		super(playerOne, playerTwo);
		moveDetails = null;
		nextPlayer = playerOne;
		playerResults = new HashMap<String, Integer>();
		Intelligence.rows = rows;
		Intelligence.columns = columns;
		board = new int[rows][columns];
		clearBoard();
		this.side = side;
	}

	private void clearBoard() {
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				board[i][j] = EMPTY;
			}
		}
	}

	@Override
	public void doPlayerMove(String player, String move)
			throws IllegalStateException {
		super.doPlayerMove(player, move);
	}

	public abstract Stack<Integer> willBeReplaced(int move, int side);

	public abstract void place(int row, int column, int side);

	public abstract int positionValue();

	public abstract Best chooseMove(int side, int bot, int top, int depth);

	public abstract boolean moveOk(int move, int side);

	public abstract String toString();

	public boolean hasMove(int side) {
		for (int i = 0; i < rows * columns; ++i) {
			if (moveOk(i, side)) {
				return true;
			}
		}
		return false;
	}

	public boolean gameOver() {
		if (!calculating) {
			this.position = positionValue();
			return this.position != UNCLEAR;
		} else {
			return false;
		}
	}

	@Override
	public void start() throws IllegalStateException {
		super.start();
		nextPlayer = playerOne;
	}

	@Override
	public String getTurnMessage() throws IllegalStateException {
		super.getTurnMessage();
		if (moveDetails != null) {
			return moveDetails;
		} else {
			return "***NEW GAME***";
		}
	}

	@Override
	public String getPlayerToMove() throws IllegalStateException {
		super.getPlayerToMove();
		return nextPlayer;
	}

	@Override
	public String getMoveDetails() throws IllegalStateException {
		super.getMoveDetails();
		if (moveDetails == null) {
			throw new IllegalStateException("No move has been done.");
		}
		return moveDetails;
	}

	@Override
	public int getPlayerResult(String player) throws IllegalStateException {
		super.getPlayerResult(player);

		return playerResults.get(player);
	}

	@Override
	public int getPlayerScore(String player) throws IllegalStateException {
		super.getPlayerResult(player);

		return playerResults.get(player);
	}

	@Override
	public String getMatchResultComment() throws IllegalStateException {
		super.getMatchResultComment();

		return "Afgelopen";
	}

	public int getMatchStatus() {
		return matchStatus;
	}

	@Override
	public Component getView() {
		return null;
	}
}